<!--
 * @description  : 市管-无证电子烟计划制定
 * @descriptionDetail: 市管-无证电子烟计划制定
 * @copyright    : 浙江烟草
 * @author       : sy
 * @create       : 2022-09-02 14:57:44
-->
<template>
  <view class="page">
    <!-- #ifdef H5-ONLY -->
    <theme-navbar
      :is-white="false"
      :title="navTitle"
      :custom-back="goBack"
      style="z-index: 99999"
    >
      <view class="nav-right-icon">
        <view @tap="handlerSure">
          确定
        </view>
      </view>
    </theme-navbar>
    <!-- #endif -->
		<!-- #ifdef H5-WECHAT || H5-EMP -->
		<fab-button :content="fContent" @fabtrigger="fabtrigger" />
		<!-- #endif -->

    <view>
      <u-search
        class="search-space"
        shape="round"
        :clearabled="true"
        :showAction="true"
        actionText="确定"
        :animation="true"
        placeholder="企业名称、法定代表人、企业住所"
        v-model="searchData.keyWord"
        @custom="handlerSearch"
      ></u-search>
      <u-dropdown class="u-p-r-30" ref="searchDropDown" @open="handlerOpen" @close="handlerClose">
        <u-dropdown-item :title="curOrgName" :options="[]" v-model="searchData.orgUuid" @change="handlerChangeItem"></u-dropdown-item>
        <u-dropdown-item title="业务类型">
          <view class="slot-ts">
            <u-cell-group :border="false">
              <u-cell-item :title="ix.bcbName" :arrow="false" v-for="ix in taskSourceOptions" :key="ix.bcbId" @click="selectTaskSubject(ix)">
                <u-icon v-if="ix.checked" slot="right-icon" size="40" :color="$theme.u_type_primary" name="checkbox-mark"></u-icon>
              </u-cell-item>
            </u-cell-group>
          </view>
        </u-dropdown-item>
      </u-dropdown>
      <u-gap class="u-gap-sty" :height="$theme.cm_gap_height" :bg-color="$theme.cm_gap_color"></u-gap>
    </view>
    <view class="content u-page u-p-t-30">
      <mescroll-list
        :i="0"
        :index="0"
        :height="getListViewHeight()"
        :load-data="loadData"
        :initLoad="false"
        ref="mescrollItem0"
      >
        <view slot-scope="d">
          <u-row style="border-bottom: 1px solid #e5e5e5; padding: 5px 0px;" @tap="handlerSelectItem(d.data)">
            <u-col span="10">
              <client-item :client="d.data"></client-item>
            </u-col>
            <u-col span="2" style="text-align:right;">
              <u-icon
                name="checkbox-mark"
                :color="$theme.u_type_primary"
                size="38"
                style="margin-right: 10px;"
                v-if="selectedUids.includes(d.data.entUuid)"
              ></u-icon>
            </u-col>
          </u-row>
        </view>
      </mescroll-list>
    </view>
    <!-- </view> -->
    <!-- 组织机构选择 -->
    <u-popup mode="bottom" height="80%" v-model="orgSelectShow" @close="handlerCloseOrg">
      <org-select :parentUuid="parentUuid" :cleanParentOrg="true" :isHybrid="true" limitNodeType="all" treeType="zm-all" @choose="handlerChooseOrg"></org-select>
    </u-popup>
  </view>
</template>

<script>
import MescrollList from "@/components/mescroll_list.vue";
import MescrollMoreMixin from "@/components/mescroll-uni/mixins/mescroll-more.js";
import OrgSelect from "@/components/org-select.vue"
import checkService from "@/service/check/check.service"
import {dateUtils,} from "@/common/util"
import ClientItem from './components/client-item.vue';
import {M_TASK_SOURCE,} from "@/service/check/mapping/enum.js";
// 企业微信集成 2023-11-9 fsx start
import fabButton from '@/components/uni-fab.vue';
// 企业微信集成 2023-11-9 fsx start
export default {
  components: {
    ClientItem,
    OrgSelect,
    MescrollList,
		fabButton
  },
  mixins: [MescrollMoreMixin,],
  data() {
    return {
      user: this.$store.state.userInfo,
      M_TASK_SOURCE,
      keyword: "",
      orgSelectShow: false,
      selectedUids: [],     // 选中项的ids
      selectedItems: [],     // 选中项
      searchData: {   // 查询参数
        keyWord: "",  // 模糊搜索关键字
        orgUuid: "",
        entType: "",
      },
      planList:[],
      newList:[],
      taskSourceOptions:[],
      parentUuid:"",
      curOrgName:"组织机构",
			// 企业微信集成 2023-11-9 fsx start
			fContent: [{
				iconPath: require('@/static/confirm.png'),
				selectedIconPath: require('@/static/confirm.png'),
				text: '确定',
				active: false,
			}],
			// 企业微信集成 2023-11-9 fsx start
    };
  },
  onLoad(opt) {
    this.planList = this.getRouteParam() ? this.getRouteParam() : [];
    // this.searchData.orgUuid = this.user.user.depart.orgUuid;// this.curUser.docOrgUuid;
    this.parentUuid = this.user.user.depart.orgUuid;
    this.curOrgName = this.user.user.depart.orgName;
  },
  created () {
    this.taskSourceOptions = this.$store.state.dicts['L_UNLIC_ECIG_ENT_ENT_TYPE'];
  },
  methods: {
    setRightBtns() {
      // 设置右侧标题
      // #ifdef H5-DINGDING
      this.$deviceApi.setRight("确定", () => {
        this.handlerSure();
      }, true, true);
      // #endif
    },
		// 企业微信集成 2023-11-9 fsx start
		fabtrigger(btn) {
			if (btn.index == 0) {
				this.handlerSure();
			}
		},
		// 企业微信集成 2023-11-9 fsx start
    getListViewHeight() {
      let p = (this.$u.sys().windowHeight - 44 - uni.upx2px(90)  - this.$u.sys().statusBarHeight) + "px";
      // #ifdef H5-DINGDING
      p = (this.$u.sys().windowHeight - uni.upx2px(90)  - this.$u.sys().statusBarHeight) + "px";
      // #endif
      return p;
    },
    selectTaskSubject(ix){
      console.log(ix,'111111')
      for(let i = 0; i < this.taskSourceOptions.length; i ++){
        if(this.taskSourceOptions[i].bcbId == ix.bcbId) {
          this.taskSourceOptions[i].checked = !this.taskSourceOptions[i].checked;
          console.log( this.taskSourceOptions[i].checked,this.taskSourceOptions,22222)
          this.searchData.entType = ix.bcbCode;
          console.log(  this.searchData.entType,333333);
          // break;
        }else{
          this.taskSourceOptions[i].checked = false;
					// 修复 业务类型选择“其他”后 变成了多选 fsx 2024-2-1 开始
          // this.searchData.entType = '';
					// 修复 业务类型选择“其他”后 变成了多选 fsx 2024-2-1 结束
        }
      }
      this.taskSourceOptions = [... this.taskSourceOptions,]
      this.$refs.searchDropDown.highlight();
      this.$refs.searchDropDown.close();
      this.handlerSearch();
    },
    handlerSearch() {
      // this.$refs['mescrollItem0'].refresh();
      let mescroll = this.getMescroll(0);
      mescroll && mescroll.triggerDownScroll();
    },
    // 选中
    handlerSelectItem(item) {
      if (this.selectedUids.includes(item.entUuid)) {
        this.selectedItems = this.selectedItems.filter(it => it.entUuid != item.entUuid);
      } else {
        this.selectedItems.push(item);
      }
      this.selectedUids = [];
      this.selectedItems.forEach(it => this.selectedUids.push(it.entUuid));
    },
    handlerOpen(idx) {
      // 组织机构 0
      idx == 0 && (this.orgSelectShow = true);
      this.$refs.searchDropDown.highlight();
    },
    handlerClose(idx) {
      this.orgSelectShow = false;
      this.$refs.searchDropDown.highlight(idx);
    },
    handlerChooseOrg(orgs) {
      if (orgs.length == 0) {
        this.$u.toast("请选择组织机构");
        return;
      }
      this.searchData.orgUuid = orgs[0].id;
      this.curOrgName = orgs[0].name;
      // this.orgSelectShow = false;
      this.$refs.searchDropDown.close();
      this.handlerSearch();
    },
    handlerCloseOrg() {
      this.$refs.searchDropDown.close();
    },
    // 选择搜索条件项
    handlerChangeItem() {
      console.log(this.searchData);
      this.handlerSearch();
    },
    // 填写年检查次数
    handlerCount(){
      this.$refs.searchDropDown.close();
      this.handlerSearch();
    },
    // 列表查询数据
    loadData(page) {
      // 调用后台接口查询数据：分页参数page.size, page.num
      let param = {
        page: page.num,
        limit: page.size,
        ...this.searchData,
      }
      return checkService.findUnlicEcigEntList(param);
    },
    // 确定
    handlerSure() {
      let self = this
      if (this.selectedItems.length == 0) {
        this.$u.toast("请选择任务");
        return;
      }
      this.planList.forEach(item => {
        self.newList.push(item.entUuid)
      })
      let idList = self.selectedItems.filter(r => {
        return self.newList.includes(r.entUuid)
      })
      if(idList.length > 0){
        this.$u.hideLoading();
        this.selectedItems.filter(item => {
          if(item.entUuid == idList[0].entUuid){
            this.$u.toast(`【${item.entName}】已存在`);
          }
        })
        return
      }
      this.planList
      this.backPrev({
        paramKey: 'temporaryTask',
        param : this.selectedItems,
      });
      this.selectedUids = this.selectedItems = [];

    },
  },
};
</script>

<style lang="scss" scoped>
.page{
	padding:0;
}

.slot-content {
  background-color: #FFFFFF;
  padding:32rpx 32rpx;
  .item-box {
    margin-bottom: 32rpx;
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;
   }
}
.slot-ts{
    background-color: #FFFFFF;

}
.txt-sty{
  line-height: 60rpx;
  padding: 0 10px;
}
.search-space{
	padding: $cm-search-padding-row $cm-search-padding-column;
	padding-bottom: 6px;
}
.u-gap-sty{
	position: relative;
}
/deep/.u-dropdown__menu__item__text{
	text-align: center;
}
</style>
